package com.zzxx.service.impl;

import com.github.pagehelper.PageHelper;
import com.zzxx.domain.Product;
import com.zzxx.mapper.ProductMapper;
import com.zzxx.service.ProductService;
import com.zzxx.utils.DateConvertor;
import com.zzxx.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Override
    public List<Product> queryAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return productMapper.findAll();
    }

    @Override
    public Product queryById(String id) {
        return productMapper.findProductById(id);
    }

    @Override
    public void updateById(Product product) {
        String departureTimeStr = product.getDepartureTimeStr();
        DateConvertor dateConvertor = new DateConvertor();
        Date date = dateConvertor.convert(departureTimeStr);
        product.setDepartureTime(date);
        productMapper.updateProductById(product);
    }

    @Override
    public void add(Product product) {
        String departureTimeStr = product.getDepartureTimeStr();
        DateConvertor dateConvertor = new DateConvertor();
        Date date = dateConvertor.convert(departureTimeStr);
        product.setDepartureTime(date);
        String id = UUIDUtils.uuid();
        product.setId(id);
        productMapper.insert(product);
    }

    @Override
    public void deleteProduct(String[] ids) {
        for(String id : ids)
            productMapper.deleteProduct(id);
    }
}
